// pages/yuyue/yuyue.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      bin_xia: null,
      bin_List: null,
      openid: null
    },
    up_bin(e) {
      let item = e.currentTarget.dataset.item;
      if (item.openid == '') {
        //添加冷冻盒
        wx.cloud.callFunction({
          name: 'updates', data: {
            type: 'up_bin',
            _id: this.data.bin_xia._id,
            index: item.index,
            openid: this.data.openid
          }
        }).then(res => {
          this.getdata();
          console.log(res);
        })
      } else {
        if (item.openid == this.data.openid) {
          //取出冷冻盒
          wx.cloud.callFunction({
            name: 'updates', data: {
              type: 'down_bin',
              _id: this.data.bin_xia._id,
              index: item.index
            }
          }).then(res => {
            console.log(res);
            this.getdata();
          })
        } else {
          wx.showToast({
            title: '已有冷冻盒',
          })
        }
      }
  
    },
  
    getdata() {
      let bin_xia = wx.getStorageSync('bin_xia');
      let openid = wx.getStorageSync('openid');
      wx.cloud.database().collection('bin_inf').doc(bin_xia._id).get().then(res => {
        console.log(res.data);
        bin_xia = res.data
        console.log(bin_xia);
        let bin_List = bin_xia.bing_box;
        for (let i = 0; i < 6; i++) {
          bin_List.push({ 'index': i, 'openid': '' })
        }
  
        let deWeightThree = () => {
          let map = new Map();
          for (let item of bin_List) {
            if (!map.has(item.index)) {
              map.set(item.index, item);
            }
          }
          return [...map.values()];
        }
        let newArr3 = deWeightThree();
        console.log('%c%s', 'color:red;', '方法三：es6,newArr3', newArr3);
        var compare = function (obj1, obj2) {
          var val1 = obj1.index;
          var val2 = obj2.index;
          if (val1 < val2) {
            return -1;
          } else if (val1 > val2) {
            return 1;
          } else {
            return 0;
          }
        }
        newArr3.sort(compare)
        console.log(newArr3);
        this.setData({ bin_List: newArr3, openid: openid, bin_xia: bin_xia })
  
      })
  
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.getdata();
    },
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
  
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
  
    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
  
    }
  })